const Koa = require('koa');
const Router = require('koa-Router')
const bodyparser = require('koa-bodyparser')
const cors = require('koa-cors')
const jwt = require('jsonwebtoken')
const Mock = require('mockjs')
const data = Mock.mock({
    'list': [
        {
            username: "张三",
            password: "123",
            url:"https://img2.woyaogexing.com/2024/05/25/85a1e2d91b95d68a!400x400.png"
        },
          {
            username: "李四",
            password: "123",
            url:"https://img2.woyaogexing.com/2024/05/25/c6a76ddd470dcdcc!400x400.png"
        },

    ]
})
const app = new Koa();
const router = new Router()

app.use(bodyparser())
app.use(cors())


app.use(router)
router.post('/api/login', ctx => {
    let { username, password } = ctx.request.body;
  
    let flag = data.list.some((item) => item.username === username && item.password === password)

    if (flag) {
        let user = data.list.find((item) => item.username === username && item.password === password)
        console.log(user);
        const token = jwt.sign({
            data: 'foobar'
        }, 'secret', { expiresIn: '1h' });
        ctx.body = {
            code: 200,
            msg: '登录成功',
            user: user,
            token: token
        }
    }
    else{
        ctx.body={
            code:404,
            msg:'账号或者密码错误'
        }
    }
})
app.listen(3090,()=>{
    console.log('服务器启动成功');
})